package com.example.customview.bezier.reddot

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PointF
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View

/**
 * 二阶贝塞尔曲线
 */
class RedDotView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defaultStyle: Int = 0
) : View(context, attributeSet, defaultStyle) {
    private var fixedPaint: Paint = Paint()
    private var mDragPaint: Paint = Paint()

    private var mBezierPaint : Paint = Paint()

    private var mFixedCenter : PointF = PointF()

    private var mFixedRadius = 40f

    //第一个控制点
    private var mFixedControllerTop : PointF = PointF()


    //第二个控制点
    private var mFixedControllerBottom : PointF = PointF()



    private var mDragCenter : PointF = PointF()
    //第一个控制点
    private var mDragControllerTop: PointF = PointF()

    //第二个控制点
    private var mDragControllerBottom : PointF = PointF()


    private var mControlX = 0f
    private var mControlY = 0f




    /**
     * 动圆信息
     */
    private var mDragRadius = 40

    /**
     * 最大距离
     */
    private var maxDistance = 200


    init {
        //辅助线
        fixedPaint.style = Paint.Style.FILL_AND_STROKE
        fixedPaint.color = Color.RED
        fixedPaint.isAntiAlias = true
        fixedPaint.textSize = 10f
        fixedPaint.isFakeBoldText = true
        fixedPaint.strokeWidth = 4f


        mBezierPaint.style = Paint.Style.STROKE
        mBezierPaint.color = Color.RED
        mBezierPaint.isAntiAlias = true



        mDragPaint.style = Paint.Style.FILL_AND_STROKE
        mDragPaint.color = Color.BLUE
        mDragPaint.isAntiAlias = true
        mDragPaint.textSize = 10f
        mDragPaint.isFakeBoldText = true

    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)


        mFixedCenter.x = w / 2f
        mFixedCenter.y = h / 2f






    }
    val path = Path()
    override fun onDraw(canvas: Canvas) {

        //先画固定圆
        canvas.drawCircle(
            mFixedCenter.x,
            mFixedCenter.y,
            mFixedRadius,
            fixedPaint
        )
        if (mDragCenter.x == 0f && mDragCenter.y == 0f){
            return
        }

        /**
         * 再画动圆
         */
        canvas.drawCircle(
            mDragCenter.x,
            mDragCenter.y,
            mDragRadius.toFloat(),
            fixedPaint
        )
        mControlX = (mDragCenter.x + mFixedCenter.x) / 2
        mControlY = (mDragCenter.y + mFixedCenter.y) / 2
        //斜率：
        val linK = MathUtil.getLineSlope(mFixedCenter,mDragCenter)


        val points = MathUtil.getIntersectionPoints(mFixedCenter, mFixedRadius, linK)
        mFixedControllerTop = points[0]
        mFixedControllerBottom = points[1]
        val pointDrag = MathUtil.getIntersectionPoints(mDragCenter, mFixedRadius, linK)

        mDragControllerTop = pointDrag[0]
        mDragControllerBottom = pointDrag[1]
        //绘制贝塞尔曲线
        path.reset()
        path.moveTo(points[0].x, points[0].y)
        path.quadTo(mControlX, mControlY, pointDrag[0].x, pointDrag[0].y)
        path.lineTo(pointDrag[1].x, pointDrag[1].y)
        path.quadTo(mControlX, mControlY, points[1].x, points[1].y)
        path.lineTo(points[0].x, points[0].y)
        canvas.drawPath(path, fixedPaint)

    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {


            }

            MotionEvent.ACTION_MOVE -> {
                mDragCenter.x = event.x
                mDragCenter.y = event.y
                /**
                 * 计算两个圆的中心点
                 */

                invalidate()
            }
        }
        return true
    }
}